﻿using hentity;
using hlog;
using System;
using System.Collections.Generic;
namespace hq {
    public  class TaskPool {
        private static Dictionary<String,ITask> taskDictionary=new Dictionary<string,ITask>();
        public  static void add( ZtTask task,String key=null ){
            if(task==null) {
                throw new Exception(key+"新线程为空");
            }
            String name=task.GetType().Name;
            if(!String.IsNullOrEmpty(key)){
                name+="_"+key;
            }
            if(taskDictionary.ContainsKey(name)){
                throw new Exception(key+"已存在");
            }
            taskDictionary[name]=task;
        }
        public static ITask get(String key) {
            if(taskDictionary.ContainsKey(key)) {
                return taskDictionary[key];
            } else {
                return null;
            }
        }
        public static T get<T>(int id) where T : ITask {
            Type type = typeof(T);
            String name = type.Name;
            name += id;
            ITask task = get(name);
            if(task == null) {
                return default;
            }
            return (T)task;
        }
         public static T getCanNull<T>(int id) where T : ITask {
            Type type = typeof(T);
            String name = type.Name;
            name += id;
            ITask task = get(name);
            if( task == null ) {
                return default(T);
            }
            return ( T ) task;
        }
        public static T get<T> () where T: ITask {
            Type type = typeof(T);
            String name = type.Name;
            ITask task = get(name);
            if(task == null) {
                throw new Exception(name + "不存在");
            }
            return (T)task;
        }
        public static void doWork<T>()where T:ZtTask  {
            T t = get<T>();
            t.work();
        
        }
        
        /***
        public static ZtTask get(Type type) {
            String name=type.Name;
            ZtTask task =  get(name);
            if(task == null) {
                throw new Exception(name + "不存在");
            }
            return task;
        }
        **/
        public static void start(params  Type[] keys) {
            foreach(Type key in keys) {
                start(key.Name);
            }
        }
        private static void start(params  String[] keys) {
            foreach(String key in keys) {
                ITask task =get(key);
                if(task==null) {
                    LoggerCommon.fileAll(key+"任务未初始化");
                } else {
                    task.start();
                }
            }
        }
        public static List<TaskStat> getStat() {
            List<TaskStat> l = new List<TaskStat>();
            foreach(var k in taskDictionary) {
                l.Add(k.Value.getStat());
            }
            return l;
        }
        public static List<ITask> getZtTask() {
            List<ITask> l = new List<ITask>();
            foreach(var k in taskDictionary) {
                l.Add(k.Value);
            }
            return l;
        }

        public static ITask getIniting() {
            List<ZtTask> l = new List<ZtTask>();
            foreach( var k in taskDictionary ) {
                if(! k.Value.isRuned() ) {
                    return k.Value;
                }
            }
            return null;
        }


        public static int count<T>() {
            int sum = 0;
            foreach(var k in taskDictionary) {
                ITask task = k.Value;
                if(task is T) {
                    sum++;
                }
            }
            return sum;
        }
    }
}
